package com.borya.dao.impl;

import com.borya.dao.ProConfDAO;
import com.borya.framework.annotation.Component;
import com.borya.framework.core.ServerSqlSession;
import com.borya.model.db.ProConf;

import org.apache.log4j.Logger;

@Component("proConfDAOImpl")
public class ProConfDAOImpl extends BaseDAOImpl implements ProConfDAO {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public ProConf getConf(String tableName) {
        try (ServerSqlSession session = getSqlDataSource().session();){
            StringBuilder sb = new StringBuilder(128);
            sb.append("select conf_name,conf_value");
            sb.append(" from ").append(tableName);
            return session.executeQuery(sb.toString(), rs-> {
            	if(rs != null){
                	ProConf pro = new ProConf();
                	while(rs.next()){
                		pro.setValue(rs.getString(1),rs.getString(2));
                	}
                	return pro;
                }
                return null;
            });
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public String getTableName(int detailType) {
        try (ServerSqlSession session = getSqlDataSource().session();){
            StringBuilder sb = new StringBuilder(128);
            sb.append("select self_table");
            sb.append(" from ").append("tf_app_table_").append(detailType);
            sb.append(" where default_table = 'sk_property_conf'");
            return session.executeQuery(sb.toString(), rs -> {
                if (rs != null) {
                    while(rs.next()){
                        String name = rs.getString(1);
                        return name;
                    }
                }
                return null;
            });
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

}
